package edu.kit.ksri.db.mturk.initializer;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import edu.kit.ksri.db.mturk.plugins.correctness.AbstractQualityControlImplementation;
import edu.kit.ksri.db.mturk.plugins.performance.TimeConstraintsManager;
import edu.kit.ksri.db.mturk.qualifications.QualificationManager;
import edu.kit.ksri.db.mturk.utils.messaging.HITResultFetcher;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
@SuppressWarnings("serial")
public class QuitFrame extends JFrame implements Runnable {

	private static final String RUNNING = "RUNNING";
	private static final String TERMINATING = "TERMINATING";
	private static final String TERMINATED = "TERMINATED";

	JLabel qualmanName = new JLabel("Qualification Manager");
	JLabel corrName = new JLabel("Correctness Plugin");
	JLabel timeName = new JLabel("Performance Plugin");
	JLabel resfetchName = new JLabel("HitResultFetcher");

	JLabel qualmanState = new JLabel("Initializing...");
	JLabel corrState = new JLabel("Initializing...");
	JLabel timeState = new JLabel("Initializing...");
	JLabel resfetchState = new JLabel("Initializing...");

	public QuitFrame() {
		super("MTurk Monitoring");
		this.setVisible(true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLayout(new GridLayout(6, 2));
		this.setSize(500, 400);

		JButton quit = new JButton("Save and Finish");
		quit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				StandaloneInitializer.setQuit(true);
			}
		});
		quit.setToolTipText("Terminates all components"
				+ " and persists their state.");

		JButton exit = new JButton("Abort");
		exit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		exit.setToolTipText("Kills the Java VM.");

		this.add(new JLabel("Component:"));
		this.add(new JLabel("State:"));
		this.add(qualmanName);
		this.add(qualmanState);
		this.add(corrName);
		this.add(corrState);
		this.add(timeName);
		this.add(timeState);
		this.add(resfetchName);
		this.add(resfetchState);
		this.add(quit);
		this.add(exit);

	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
			if (StandaloneInitializer.isStarting())
				continue;
			// HITResultFetcher
			if (HITResultFetcher.getInstance().isRunning()) {
				resfetchState.setText(RUNNING);
				resfetchState.setForeground(Color.green);
			} else if (HITResultFetcher.getInstance().isTerminated()) {
				resfetchState.setText(TERMINATED);
				resfetchState.setForeground(Color.red);
			} else {
				resfetchState.setText(TERMINATING);
				resfetchState.setForeground(Color.yellow);
			}
			// TimeConstraintsManager
			if (TimeConstraintsManager.isRunning()) {
				timeState.setText(RUNNING);
				timeState.setForeground(Color.green);
			} else if (TimeConstraintsManager.isTerminated()) {
				timeState.setText(TERMINATED);
				timeState.setForeground(Color.red);
			} else {
				timeState.setText(TERMINATING);
				timeState.setForeground(Color.yellow);
			}
			// Correctness Plugin
			if (AbstractQualityControlImplementation.isRunning()) {
				corrState.setText(RUNNING);
				corrState.setForeground(Color.green);
			} else if (AbstractQualityControlImplementation.isTerminated()) {
				corrState.setText(TERMINATED);
				corrState.setForeground(Color.red);
			} else {
				corrState.setText(TERMINATING);
				corrState.setForeground(Color.yellow);
			}
			// QualificationManager
			if (QualificationManager.isRunning()) {
				qualmanState.setText(RUNNING);
				qualmanState.setForeground(Color.green);
			} else if (QualificationManager.isTerminated()) {
				qualmanState.setText(TERMINATED);
				qualmanState.setForeground(Color.red);
			} else {
				qualmanState.setText(TERMINATING);
				qualmanState.setForeground(Color.yellow);
			}
		}
	}

}
